package net.lzzy.lxq.controller;

import net.lzzy.lxq.entities.Attendance;
import net.lzzy.lxq.entities.MonitorDevice;
import net.lzzy.lxq.entities.UserBean;
import net.lzzy.lxq.entities.UserDevice;
import net.lzzy.lxq.server.AttendanceService;
import net.lzzy.lxq.server.MonitorDeviceService;
import net.lzzy.lxq.server.UserDeviceService;
import net.lzzy.lxq.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController()
public class AdminController {

    @Autowired
    UserService userService;
    @Autowired
    AttendanceService attendanceService;
    @Autowired
    UserDeviceService deviceService;
    @Autowired
    MonitorDeviceService monitorService;

    @GetMapping("/admin/get_all_personnel")
    public List<UserBean> getUsers(){
        return userService.getAll();
    }
    @GetMapping("/admin/get_all_attendance")
    public List<Attendance> getAllAttendance(){
        List<Attendance> attendances=attendanceService.getAll();
        for(Attendance attendance:attendances){
            long time=attendance.getSwipeTime();
            String formatted=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new Date(time));
            attendance.setFormatted(formatted);
        }
        return attendances;
    }
    @GetMapping("/admin/get_all_user_device")
    public List<UserDevice> getUserDevices(){
        return deviceService.getUserDevices();
    }
    @GetMapping("/admin/get_all_monitor")
    public List<MonitorDevice> getMonitors(){
        return monitorService.getAllMonitors();
    }

    @PostMapping("/admin/add_personnel")
    public Map<String,Object> addPersonnel(@RequestBody UserBean newUser){
        int resultInt=userService.addUser(newUser);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("code",resultInt);
        System.out.println(newUser.toString());
        return resultMap;
    }
}
